package com.app.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.app.mapper.UserMapper;
import com.app.pojo.Complaint;
import com.app.pojo.User;
import com.app.service.IComplaintService;
import com.app.util.Result;
import com.app.vo.ComplaintVo;
import com.app.vo.PublicAnnouncementVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 投诉建议 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2024-07-29
 */
@Api(tags = "投诉建议相关接口")
@RestController
@RequestMapping("/complaint")
public class ComplaintController {

    @Autowired
    private IComplaintService complaintService;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "分页查询我的投诉建议列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, dataType = "Integer")
    })
    @GetMapping("/getMyComplaintList")
    public Result getMyComplaintList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                            HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error( "token不存在");
        }
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken == null) {
            Result result = new Result(401, "登录失效");
            return result;
        }
        String userId = loginIdByToken.toString();
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error( "用户不存在");
        }
        ComplaintVo vo = complaintService.getMyComplaintList( page, limit, Integer.valueOf(userId));
        return Result.success(vo);
    }



    @ApiOperation(value = "新增投诉建议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "complaint", value = "投诉建议对象", required = true, dataType = "Complaint")
    })
    @PostMapping("/addComplaint")
    public Result addComplaint(@RequestBody Complaint complaint, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error( "token不存在");
        }
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken == null) {
            Result result = new Result(401, "登录失效");
            return result;
        }
        String userId = loginIdByToken.toString();
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error( "用户不存在");
        }
        complaintService.addComplaint(complaint, Integer.valueOf(userId));
        return Result.success("新增成功！");
    }

}

